//
//  BJBaseNetworking.m
//  
//
//  Created by  on 2023/3/2.
//

#import "BJBaseNetworking.h"

@interface BJBaseNetworking ()

@property (nonatomic) AFHTTPSessionManager *manager;

@end


@implementation BJBaseNetworking


// 通过json格式提交参数
- (id)POST:(NSString *)path parameters:(id)parameters completionHandler:(void (^)(id , NSError * ))completionHandler{
    self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSMutableDictionary *headers = [NSMutableDictionary dictionary];

    [headers addEntriesFromDictionary:[[BJConfigManager sharedManager]getDefHeaders]];
    [headers setValue:@"application/json" forKey:@"Content-Type"];
    [headers setValue:@"application/json" forKey:@"Accept"];
    [headers setValue:[NSString stringWithFormat:@"Bearer%@",[[BJUserManager sharedManager] getUserToken]] forKey:@"Authorization"];

    return [self.manager POST:path parameters:parameters headers:headers progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        ASLog(@"NetLog_post_success=%@",task.currentRequest.URL.absoluteString);
        !completionHandler?:completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        ASLog(@"NetLog_post_failure=%@",task.currentRequest.URL.absoluteString);
        !completionHandler?:completionHandler(nil, error);
        NSLog(@"failure:%@",error);
    }];
}

// 通过表单式提交参数
- (id)POSTForLogin:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id , NSError *))completionHandler{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kBasePath]];
    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
    [headers addEntriesFromDictionary:[[BJConfigManager sharedManager]getDefHeaders]];
    [headers setValue:[NSString stringWithFormat:@"Bearer%@",[[BJUserManager sharedManager] getUserToken]] forKey:@"Authorization"];

    return [manager POST:path parameters:parameters headers:headers progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        ASLog(@"NetLog_post_success=%@",task.currentRequest.URL.absoluteString);
        !completionHandler?:completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        ASLog(@"NetLog_post_failure=%@",task.currentRequest.URL.absoluteString);
        if ([error.domain isEqualToString:AFURLResponseSerializationErrorDomain]){
            //NSData *errorData = [error.userInfo objectForKey:AFNetworkingOperationFailingURLResponseDataErrorKey];
            //NSString *str = [[NSString alloc]initWithData:errorData encoding:NSUTF8StringEncoding];
            //ASLog(@"error--str=%@",str);
        }
        !completionHandler ?: completionHandler(nil, error);
    }];
}

- (id)GET:(NSString *)path parameters:(NSDictionary *)parameters completionHandler:(void (^)(id , NSError *p))completionHandler{
    NSMutableDictionary *headers = [NSMutableDictionary dictionary];
    [headers addEntriesFromDictionary:[[BJConfigManager sharedManager]getDefHeaders]];
    [headers setValue:[NSString stringWithFormat:@"Bearer%@",[[BJUserManager sharedManager] getUserToken]] forKey:@"Authorization"];
    return [self.manager GET:path parameters:parameters headers:headers progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        ASLog(@"NetLog_get_success=%@",task.currentRequest.URL.absoluteString);
        !completionHandler?:completionHandler(responseObject, nil);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        ASLog(@"NetLog_get_failure=%@",task.currentRequest.URL.absoluteString);
        !completionHandler ?: completionHandler(nil, error);
    }];
}


- (AFHTTPSessionManager *)manager{
    if (_manager == nil) {
        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kBasePath]];
    }
    return _manager;
}






@end
